import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class Grep {

    public static boolean find(String file, String str) {
	try {
	    Reader in = new FileReader(file);
	    char[] data = str.toCharArray();
	    int place = 0;
	    int c;
	    while ((c = in.read()) != -1) {
		if (c == data[place]) {
		    place++;
		    if (place == data.length) {
			in.close();
			return true;
		    }
		} else
		    place = 0;
	    }
	    in.close();
	    return false;
	} catch (Exception e) {
	    return false;
	}
    }

    public static String getLine(String file, String str) {
	try {
	    FileInputStream fstream = new FileInputStream(file);
	    DataInputStream in = new DataInputStream(fstream);
	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    String line;

	    while ((line = br.readLine()) != null) {
		if (line.contains(str))
		    break;
	    }

	    in.close();
	    if (line == null)
		line = "";
	    return line;
	} catch (Exception e) {
	    return "";
	}
    }

    public static String getLineContent(String file, int number) {
	try {
	    FileInputStream fstream = new FileInputStream(file);
	    DataInputStream in = new DataInputStream(fstream);
	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    String line;

	    int count = 0;
	    while ((line = br.readLine()) != null && ++count < number)
		;

	    in.close();
	    if (line == null)
		line = "";
	    return line;
	} catch (Exception e) {
	    return "";
	}
    }

    public static List<Integer> getStringPositions(String file, String str) {
	List<Integer> pos = new ArrayList<Integer>();
	try {
	    FileInputStream fstream = new FileInputStream(file);
	    DataInputStream in = new DataInputStream(fstream);
	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    String line;
	    int line_nr = 1;

	    while ((line = br.readLine()) != null) {
		if (line.contains(str))
		    pos.add(line_nr);

		line_nr++;
	    }

	    in.close();
	} catch (Exception e) {
	}
	return pos;
    }
}
